package cc.rengu.igas.cops.core.service.trans.intelliroute;

import cc.rengu.igas.cops.common.constant.CopsProductVersion;
import cc.rengu.igas.cops.core.model.ProcAddition;
import cc.rengu.igas.route.facade.request.IntelligencePaymentSignVerifyRequest;
import cc.rengu.igas.route.facade.response.IntelligencePaymentSignVerifyResponse;
import cc.rengu.igas.route.facade.result.Result;
import cc.rengu.oltp.service.base.BizBaseService;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.model.RiskMonitorInfo;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * 智能路由标准对外收款签约确认（代收签约确认）
 * 此交易一般为实际的签约交易，并附带需要校验的信息，例如短信验证码等
 *
 * @author lizhangtao
 * @version 1.0
 * @date 2020/05/02
 */
public class IntelligencePaymentSignVerifyService extends BizBaseService {
    private static final String DUBBO_BEAN = "smartRouteService";
    private static final String DUBBO_METHOD = "paymentSignVerify";

    @Override
    protected Object beforeBizProcess() throws Exception {
        IntelligencePaymentSignVerifyRequest request = new IntelligencePaymentSignVerifyRequest();
        ConvertUtil.convertOutput(request);
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        Class c = Class.forName("cc.rengu.igas.cops.core.model." + CopsProductVersion.VERSION + ".ProcAdditionInfoImpl");
        ProcAddition procAddition = (ProcAddition) c.newInstance();
        request.setAdditionInfo(procAddition.procAdditionInfo(request.getHeader().getInstId(), request.getAdditionInfo()));
        if (!StringUtil.isEmptyOrNull(request.getAdditionInfo())) {
            xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.REMARK4, request.getAdditionInfo());
        }
        return request;
    }

    @Override
    protected BizResponse callBizService(Object request) throws Exception {
        BizResponse bizResponse = new BizResponse();
        IntelligencePaymentSignVerifyRequest signVerifyRequest = (IntelligencePaymentSignVerifyRequest) request;
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        //签约确认不登记订单
        //3. 调用route模块dubbo服务-对外收款签约确认（代收签约确认）
        DubboService dubboService = new DubboServiceImpl();
        Result<IntelligencePaymentSignVerifyResponse> result = (Result<IntelligencePaymentSignVerifyResponse>) dubboService.callDubboService(DUBBO_BEAN, DUBBO_METHOD, signVerifyRequest);
        if (null == result || null == result.getResult()) {
            bizResponse.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
            bizResponse.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
        } else {
            ConvertUtil.convertInput(result.getResult());
            String dstChannelId = result.getResult().getDstChannelId();
            rglog.info("dstChannelId={}", dstChannelId);
            if (!StringUtil.isEmptyOrNull(dstChannelId)) {
                bizResponse.setRspSysId(dstChannelId);
            } else {
                bizResponse.setRspSysId("IGAS");
            }
            //应答的addition存储到remark5
            String respAddition = result.getResult().getAdditionInfo();
            if (!StringUtil.isEmptyOrNull(respAddition) && !"{}".equals(respAddition)) {
                try {
                    JSONObject jsonObjectReq = null;
                    if (!StringUtil.isEmptyOrNull(signVerifyRequest.getAdditionInfo())) {
                        jsonObjectReq = JSON.parseObject(signVerifyRequest.getAdditionInfo());
                    }
                    //去除请求中相同的字段只存储不同的,防止出现长度过长
                    JSONObject jsonObjectRspTmp = JSON.parseObject(respAddition);
                    if (null != jsonObjectReq && null != jsonObjectRspTmp) {
                        jsonObjectReq.forEach((key, value) -> {
                            jsonObjectRspTmp.remove(key);
                        });
                        if (!jsonObjectRspTmp.isEmpty()) {
                            respAddition = jsonObjectRspTmp.toJSONString();
                            if ("{}".equals(respAddition)) {
                                respAddition = "";
                            }
                        }
                    }
                    xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.REMARK5, respAddition);
                } catch (Exception e) {
                    rglog.info(cc.rengu.utility.base.StringUtil.ExceptionToString(e));
                }
            }
            if (!StringUtil.isEmptyOrNull(signVerifyRequest.getAdditionInfo())) {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.REMARK4, signVerifyRequest.getAdditionInfo());
            }
            bizResponse.setRespDesc(result.getResult().getRespDesc());
            bizResponse.setRespCode(result.getResult().getRespCode());
            bizResponse.setResult(result.getResult());
        }
        return bizResponse;
    }

    @Override
    protected void afterBizProcess(BizResponse response) throws Exception {

    }

    @Override
    protected RiskMonitorInfo getRiskMonitorInfo() {
        return null;
    }
}
